﻿using System;
using System.Configuration;
using System.Net.Mail;
using System.Text;
using System.Web;
using System.Web.Security;
using System.Web.UI.WebControls;
using AzureQueue;

namespace WebPages.Account
{
    public partial class Login : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterHyperLink.NavigateUrl = "Register.aspx?ReturnUrl=" + HttpUtility.UrlEncode(Request.QueryString["ReturnUrl"]);
        }

        protected void ValidateIfLockedOut(object source, ServerValidateEventArgs args)
        {
            try
            {
                var user = Membership.GetUser(LoginUser.UserName, false);
                if (user == null) return;
                args.IsValid = !user.IsLockedOut;
            }
            catch (Exception)
            {
                args.IsValid = true;
            }
        }

        void SendValidatationEmail(MembershipUser user)
        {
            var userName = user.UserName;
            var emailAddress = user.Email;

            // Lets get the user's id
            var userId = (Guid)Membership.GetUser(userName).ProviderUserKey;

            // Now lets create an email message

            var emailMessage = new StringBuilder();
            var header = Resources.Email.EmailHeader.Replace("{0}", "Thank you for creating a MyApp account!");
            emailMessage.Append(header);
            emailMessage.Append(String.Format("Hello, {0}.<br />Please click the below link to activate your MyApp account and complete your account registration:<br /><br />", userName));
            var url = string.Format("http://www.MyApp.com/Account/ActivateAccount.aspx?Id={0}", userId);
            emailMessage.Append(string.Format("<a href='{0}'>{0}</a><br /><br />", url));
            emailMessage.Append("If your e-mail client does not allow links then copy & paste the above url into your browser address bar.");
            var footer = String.Format(Resources.Email.EmailFooter, "This e-mail was sent because someone registered an user account at <a href=\"http://www.MyApp.com\">www.MyApp.com</a> using this e-mail address.");
            emailMessage.Append(footer);
            var fromEmail = ConfigurationManager.AppSettings["fromEmail"];

            // Send the email
            var queueDataSource = new AzureQueueDataSource();
            queueDataSource.SendEmail(fromEmail, emailAddress, "Activate your MyApp account", emailMessage.ToString());
        }

        protected void ValidateIfApproved(object source, ServerValidateEventArgs args)
        {
            try
            {
                var user = Membership.GetUser(LoginUser.UserName, false);
                if (user == null) return;
                if (!user.IsApproved) SendValidatationEmail(user);
                args.IsValid = user.IsApproved;
            }
            catch (Exception)
            {
                args.IsValid = true;
            }
        }
    }
}
